<?php

class ActionController {
	
	public static function insertEventLocation($eventLocationName, $eventLocationAdres, $eventLocationCity, $eventLocationLocationName, $eventLocationDescription, $locationX, $locationY, $eventName, $eventDescription, $eventValue, $user, $tags, $categores) {
	
	    $queryString = "insert into ".Constans::$EVENTLOCATION." set name='$eventLocationName', adress='$eventLocationAdres', city='$eventLocationCity', location='$eventLocationLocationName', description='$eventLocationDescription', x='$locationX', y='$locationY'";
		
		$result = DataBaseController::execute($queryString);
		
		$row = DataBaseController::row("SELECT * FROM ".Constans::$EVENTLOCATION." WHERE x='$locationX' and y='$locationY'");
		
		$eventLocationId = $row['eventlocation_id'];
		
		ActionController.insertEvent($eventLocationId, $eventName, $eventDescription, $eventValue, $user, $tags, $categores);
		
		return MessageController::getStatusMessage($result);
	}
	
	public static function insertEvent($eventLocationId, $name, $description, $value, $user, $tag, $categores) {

		$result = DataBaseController::execute("insert into ".Constans::$EVENT." set eventlocation_id='$eventLocationId', name='$name', description='$description', value='$value', user='$user'");

		$tags = explode("|", $tag);

		foreach ($tags as $t) {
			$row = DataBaseController::row("SELECT * FROM ".Constans::$TAG." WHERE name='$t'");
			if($row){
				$tagId = $row['tag_id'];
				$row = DataBaseController::row("SELECT * FROM ".Constans::$EVENT." WHERE eventlocation_id='$eventLocationId' and name='$name' and description='$description' and value='$value' and user='$user'");
				$eventId = $row['event_id']; 
				DataBaseController::execute("insert into ".Constans::$TAGMAPPING." set event_id='$eventId', tag_id='$tagId'");
			}
		}
		
		//remove last char
		$categores = substr_replace($categores ,"",-1);
		
		//string to array
		$category= explode("|", $categores);
		
		foreach ($category as $categoryId) {
			DataBaseController::execute("insert into ".Constans::$CATEGORYMAPPING." set event_id='$eventId', category_id='$categoryId'");
		}
			
		return MessageController::getStatusMessage($result);
	}

	public static function getTag() {
		
		$result = DataBaseController::execute("SELECT * FROM ".Constans::$TAG);

		$outputXml = XmlFactory::ct('tags', 'o');

		while($data=mysql_fetch_assoc($result)){
			$outputXml .= XmlFactory::ct('tag', 'o').$data['name'].XmlFactory::ct('tag', 'c');
		}

		$outputXml .= XmlFactory::ct('tags', 'c');

		return MessageController::returnMessage($outputXml);
	}

	public static function getCategory() {	
		
		$outputXml = XmlFactory::ct('categores', 'o');
		
		$level = 1;
		
		$result = DataBaseController::execute("SELECT * FROM ".Constans::$CATEGORY." WHERE level='$level' ORDER BY name");	
		
		while($data=mysql_fetch_assoc($result)){
			$outputXml .= XmlFactory::ct('categoryItem', 'o');
			
			$outputXml .= XmlFactory::ct('category', 'id="'.$data['category_id']).$data['name'].XmlFactory::ct('category', 'c');
			
			$category_id = $data['category_id'];
			$levelResult = DataBaseController::execute("SELECT * FROM ".Constans::$CATEGORY." WHERE parentcategory='$category_id' ORDER BY name");	
			while($levelData=mysql_fetch_assoc($levelResult)){
				$outputXml .= XmlFactory::ct('level', 'id="'.$levelData['category_id']).$levelData['name'].XmlFactory::ct('level', 'c');
				$subCategory_id = $levelData['category_id'];
				
				$subLevelResult = DataBaseController::execute("SELECT * FROM ".Constans::$CATEGORY." WHERE parentcategory='$subCategory_id' ORDER BY name");	
				while($subLevelData=mysql_fetch_assoc($subLevelResult)){
					$outputXml .= XmlFactory::ct('sublevel', 'id="'.$subLevelData['category_id']).$subLevelData['name'].XmlFactory::ct('sublevel', 'c');
				}
			}
			$outputXml .= XmlFactory::ct('categoryItem', 'c');
		}
		
		$outputXml .= XmlFactory::ct('categores', 'c');
		
		return MessageController::returnMessage($outputXml);
	}
	
	public static function search($text, $valueMin, $valueMax) {
		$resultEvent = array();
		$resultCount = 0;
		
		$tags = explode(" ", $text);
		foreach ($tags as $element) {
			$resultEvent[$resultCount] = SearchController::searchCondition($element);
			$resultCount++;
		}
		
		if (count($resultEvent)==2){			
			$resultNewEvent = ActionController::sharedParts($resultEvent[0], $resultEvent[1]);
		}else{
			$resultNewEvent = $resultEvent[0];
		}
		
		$resultNewEvent = SearchController::valueCondition($resultNewEvent, $valueMin, $valueMax);
		
		if(count($resultNewEvent)!=0){
			
			$outputXml = XmlFactory::ct('events', 'o');
			
			foreach ($resultNewEvent as $element) {
				$outputXml .= XmlFactory::buildSearchXml($element);
			}
			
			$outputXml .= XmlFactory::ct('events', 'c');
			
			return MessageController::returnMessage($outputXml);
		}else{
			return MessageController::returnMessage("Nothing found.");
		}
	}
	
	protected static function sharedParts($one, $two) {
		if (count($one)==0){
			return $two;
		}
		if (count($two)==0){
			return $one;
		}	
		$result = array();	
		$count = 0;	
		foreach ($one as $oneElement) {
			foreach ($two as $twoElement) {
				if($oneElement==$twoElement){
					$result[$count] = $oneElement;
					$count++;
				}
			}
		}
		return $result;
	}   

}

?>